// Code generated by entc, DO NOT EDIT.

package ent

import (
	"context"
	"errors"
	"fmt"
	"time"

	"entgo.io/ent/dialect/sql"
	"entgo.io/ent/dialect/sql/sqlgraph"
	"entgo.io/ent/schema/field"
	"github.com/oceanbase/configserver/ent/obcluster"
	"github.com/oceanbase/configserver/ent/predicate"
)

// ObClusterUpdate is the builder for updating ObCluster entities.
type ObClusterUpdate struct {
	config
	hooks    []Hook
	mutation *ObClusterMutation
}

// Where appends a list predicates to the ObClusterUpdate builder.
func (ocu *ObClusterUpdate) Where(ps ...predicate.ObCluster) *ObClusterUpdate {
	ocu.mutation.Where(ps...)
	return ocu
}

// SetCreateTime sets the "create_time" field.
func (ocu *ObClusterUpdate) SetCreateTime(t time.Time) *ObClusterUpdate {
	ocu.mutation.SetCreateTime(t)
	return ocu
}

// SetNillableCreateTime sets the "create_time" field if the given value is not nil.
func (ocu *ObClusterUpdate) SetNillableCreateTime(t *time.Time) *ObClusterUpdate {
	if t != nil {
		ocu.SetCreateTime(*t)
	}
	return ocu
}

// SetUpdateTime sets the "update_time" field.
func (ocu *ObClusterUpdate) SetUpdateTime(t time.Time) *ObClusterUpdate {
	ocu.mutation.SetUpdateTime(t)
	return ocu
}

// SetName sets the "name" field.
func (ocu *ObClusterUpdate) SetName(s string) *ObClusterUpdate {
	ocu.mutation.SetName(s)
	return ocu
}

// SetObClusterID sets the "ob_cluster_id" field.
func (ocu *ObClusterUpdate) SetObClusterID(i int64) *ObClusterUpdate {
	ocu.mutation.ResetObClusterID()
	ocu.mutation.SetObClusterID(i)
	return ocu
}

// AddObClusterID adds i to the "ob_cluster_id" field.
func (ocu *ObClusterUpdate) AddObClusterID(i int64) *ObClusterUpdate {
	ocu.mutation.AddObClusterID(i)
	return ocu
}

// SetType sets the "type" field.
func (ocu *ObClusterUpdate) SetType(s string) *ObClusterUpdate {
	ocu.mutation.SetType(s)
	return ocu
}

// SetRootserviceJSON sets the "rootservice_json" field.
func (ocu *ObClusterUpdate) SetRootserviceJSON(s string) *ObClusterUpdate {
	ocu.mutation.SetRootserviceJSON(s)
	return ocu
}

// Mutation returns the ObClusterMutation object of the builder.
func (ocu *ObClusterUpdate) Mutation() *ObClusterMutation {
	return ocu.mutation
}

// Save executes the query and returns the number of nodes affected by the update operation.
func (ocu *ObClusterUpdate) Save(ctx context.Context) (int, error) {
	var (
		err      error
		affected int
	)
	ocu.defaults()
	if len(ocu.hooks) == 0 {
		if err = ocu.check(); err != nil {
			return 0, err
		}
		affected, err = ocu.sqlSave(ctx)
	} else {
		var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
			mutation, ok := m.(*ObClusterMutation)
			if !ok {
				return nil, fmt.Errorf("unexpected mutation type %T", m)
			}
			if err = ocu.check(); err != nil {
				return 0, err
			}
			ocu.mutation = mutation
			affected, err = ocu.sqlSave(ctx)
			mutation.done = true
			return affected, err
		})
		for i := len(ocu.hooks) - 1; i >= 0; i-- {
			if ocu.hooks[i] == nil {
				return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)")
			}
			mut = ocu.hooks[i](mut)
		}
		if _, err := mut.Mutate(ctx, ocu.mutation); err != nil {
			return 0, err
		}
	}
	return affected, err
}

// SaveX is like Save, but panics if an error occurs.
func (ocu *ObClusterUpdate) SaveX(ctx context.Context) int {
	affected, err := ocu.Save(ctx)
	if err != nil {
		panic(err)
	}
	return affected
}

// Exec executes the query.
func (ocu *ObClusterUpdate) Exec(ctx context.Context) error {
	_, err := ocu.Save(ctx)
	return err
}

// ExecX is like Exec, but panics if an error occurs.
func (ocu *ObClusterUpdate) ExecX(ctx context.Context) {
	if err := ocu.Exec(ctx); err != nil {
		panic(err)
	}
}

// defaults sets the default values of the builder before save.
func (ocu *ObClusterUpdate) defaults() {
	if _, ok := ocu.mutation.UpdateTime(); !ok {
		v := obcluster.UpdateDefaultUpdateTime()
		ocu.mutation.SetUpdateTime(v)
	}
}

// check runs all checks and user-defined validators on the builder.
func (ocu *ObClusterUpdate) check() error {
	if v, ok := ocu.mutation.ObClusterID(); ok {
		if err := obcluster.ObClusterIDValidator(v); err != nil {
			return &ValidationError{Name: "ob_cluster_id", err: fmt.Errorf(`ent: validator failed for field "ObCluster.ob_cluster_id": %w`, err)}
		}
	}
	return nil
}

func (ocu *ObClusterUpdate) sqlSave(ctx context.Context) (n int, err error) {
	_spec := &sqlgraph.UpdateSpec{
		Node: &sqlgraph.NodeSpec{
			Table:   obcluster.Table,
			Columns: obcluster.Columns,
			ID: &sqlgraph.FieldSpec{
				Type:   field.TypeInt,
				Column: obcluster.FieldID,
			},
		},
	}
	if ps := ocu.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := ocu.mutation.CreateTime(); ok {
		_spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
			Type:   field.TypeTime,
			Value:  value,
			Column: obcluster.FieldCreateTime,
		})
	}
	if value, ok := ocu.mutation.UpdateTime(); ok {
		_spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
			Type:   field.TypeTime,
			Value:  value,
			Column: obcluster.FieldUpdateTime,
		})
	}
	if value, ok := ocu.mutation.Name(); ok {
		_spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
			Type:   field.TypeString,
			Value:  value,
			Column: obcluster.FieldName,
		})
	}
	if value, ok := ocu.mutation.ObClusterID(); ok {
		_spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
			Type:   field.TypeInt64,
			Value:  value,
			Column: obcluster.FieldObClusterID,
		})
	}
	if value, ok := ocu.mutation.AddedObClusterID(); ok {
		_spec.Fields.Add = append(_spec.Fields.Add, &sqlgraph.FieldSpec{
			Type:   field.TypeInt64,
			Value:  value,
			Column: obcluster.FieldObClusterID,
		})
	}
	if value, ok := ocu.mutation.GetType(); ok {
		_spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
			Type:   field.TypeString,
			Value:  value,
			Column: obcluster.FieldType,
		})
	}
	if value, ok := ocu.mutation.RootserviceJSON(); ok {
		_spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
			Type:   field.TypeString,
			Value:  value,
			Column: obcluster.FieldRootserviceJSON,
		})
	}
	if n, err = sqlgraph.UpdateNodes(ctx, ocu.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{obcluster.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{err.Error(), err}
		}
		return 0, err
	}
	return n, nil
}

// ObClusterUpdateOne is the builder for updating a single ObCluster entity.
type ObClusterUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *ObClusterMutation
}

// SetCreateTime sets the "create_time" field.
func (ocuo *ObClusterUpdateOne) SetCreateTime(t time.Time) *ObClusterUpdateOne {
	ocuo.mutation.SetCreateTime(t)
	return ocuo
}

// SetNillableCreateTime sets the "create_time" field if the given value is not nil.
func (ocuo *ObClusterUpdateOne) SetNillableCreateTime(t *time.Time) *ObClusterUpdateOne {
	if t != nil {
		ocuo.SetCreateTime(*t)
	}
	return ocuo
}

// SetUpdateTime sets the "update_time" field.
func (ocuo *ObClusterUpdateOne) SetUpdateTime(t time.Time) *ObClusterUpdateOne {
	ocuo.mutation.SetUpdateTime(t)
	return ocuo
}

// SetName sets the "name" field.
func (ocuo *ObClusterUpdateOne) SetName(s string) *ObClusterUpdateOne {
	ocuo.mutation.SetName(s)
	return ocuo
}

// SetObClusterID sets the "ob_cluster_id" field.
func (ocuo *ObClusterUpdateOne) SetObClusterID(i int64) *ObClusterUpdateOne {
	ocuo.mutation.ResetObClusterID()
	ocuo.mutation.SetObClusterID(i)
	return ocuo
}

// AddObClusterID adds i to the "ob_cluster_id" field.
func (ocuo *ObClusterUpdateOne) AddObClusterID(i int64) *ObClusterUpdateOne {
	ocuo.mutation.AddObClusterID(i)
	return ocuo
}

// SetType sets the "type" field.
func (ocuo *ObClusterUpdateOne) SetType(s string) *ObClusterUpdateOne {
	ocuo.mutation.SetType(s)
	return ocuo
}

// SetRootserviceJSON sets the "rootservice_json" field.
func (ocuo *ObClusterUpdateOne) SetRootserviceJSON(s string) *ObClusterUpdateOne {
	ocuo.mutation.SetRootserviceJSON(s)
	return ocuo
}

// Mutation returns the ObClusterMutation object of the builder.
func (ocuo *ObClusterUpdateOne) Mutation() *ObClusterMutation {
	return ocuo.mutation
}

// Select allows selecting one or more fields (columns) of the returned entity.
// The default is selecting all fields defined in the entity schema.
func (ocuo *ObClusterUpdateOne) Select(field string, fields ...string) *ObClusterUpdateOne {
	ocuo.fields = append([]string{field}, fields...)
	return ocuo
}

// Save executes the query and returns the updated ObCluster entity.
func (ocuo *ObClusterUpdateOne) Save(ctx context.Context) (*ObCluster, error) {
	var (
		err  error
		node *ObCluster
	)
	ocuo.defaults()
	if len(ocuo.hooks) == 0 {
		if err = ocuo.check(); err != nil {
			return nil, err
		}
		node, err = ocuo.sqlSave(ctx)
	} else {
		var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
			mutation, ok := m.(*ObClusterMutation)
			if !ok {
				return nil, fmt.Errorf("unexpected mutation type %T", m)
			}
			if err = ocuo.check(); err != nil {
				return nil, err
			}
			ocuo.mutation = mutation
			node, err = ocuo.sqlSave(ctx)
			mutation.done = true
			return node, err
		})
		for i := len(ocuo.hooks) - 1; i >= 0; i-- {
			if ocuo.hooks[i] == nil {
				return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)")
			}
			mut = ocuo.hooks[i](mut)
		}
		if _, err := mut.Mutate(ctx, ocuo.mutation); err != nil {
			return nil, err
		}
	}
	return node, err
}

// SaveX is like Save, but panics if an error occurs.
func (ocuo *ObClusterUpdateOne) SaveX(ctx context.Context) *ObCluster {
	node, err := ocuo.Save(ctx)
	if err != nil {
		panic(err)
	}
	return node
}

// Exec executes the query on the entity.
func (ocuo *ObClusterUpdateOne) Exec(ctx context.Context) error {
	_, err := ocuo.Save(ctx)
	return err
}

// ExecX is like Exec, but panics if an error occurs.
func (ocuo *ObClusterUpdateOne) ExecX(ctx context.Context) {
	if err := ocuo.Exec(ctx); err != nil {
		panic(err)
	}
}

// defaults sets the default values of the builder before save.
func (ocuo *ObClusterUpdateOne) defaults() {
	if _, ok := ocuo.mutation.UpdateTime(); !ok {
		v := obcluster.UpdateDefaultUpdateTime()
		ocuo.mutation.SetUpdateTime(v)
	}
}

// check runs all checks and user-defined validators on the builder.
func (ocuo *ObClusterUpdateOne) check() error {
	if v, ok := ocuo.mutation.ObClusterID(); ok {
		if err := obcluster.ObClusterIDValidator(v); err != nil {
			return &ValidationError{Name: "ob_cluster_id", err: fmt.Errorf(`ent: validator failed for field "ObCluster.ob_cluster_id": %w`, err)}
		}
	}
	return nil
}

func (ocuo *ObClusterUpdateOne) sqlSave(ctx context.Context) (_node *ObCluster, err error) {
	_spec := &sqlgraph.UpdateSpec{
		Node: &sqlgraph.NodeSpec{
			Table:   obcluster.Table,
			Columns: obcluster.Columns,
			ID: &sqlgraph.FieldSpec{
				Type:   field.TypeInt,
				Column: obcluster.FieldID,
			},
		},
	}
	id, ok := ocuo.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "ObCluster.id" for update`)}
	}
	_spec.Node.ID.Value = id
	if fields := ocuo.fields; len(fields) > 0 {
		_spec.Node.Columns = make([]string, 0, len(fields))
		_spec.Node.Columns = append(_spec.Node.Columns, obcluster.FieldID)
		for _, f := range fields {
			if !obcluster.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != obcluster.FieldID {
				_spec.Node.Columns = append(_spec.Node.Columns, f)
			}
		}
	}
	if ps := ocuo.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := ocuo.mutation.CreateTime(); ok {
		_spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
			Type:   field.TypeTime,
			Value:  value,
			Column: obcluster.FieldCreateTime,
		})
	}
	if value, ok := ocuo.mutation.UpdateTime(); ok {
		_spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
			Type:   field.TypeTime,
			Value:  value,
			Column: obcluster.FieldUpdateTime,
		})
	}
	if value, ok := ocuo.mutation.Name(); ok {
		_spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
			Type:   field.TypeString,
			Value:  value,
			Column: obcluster.FieldName,
		})
	}
	if value, ok := ocuo.mutation.ObClusterID(); ok {
		_spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
			Type:   field.TypeInt64,
			Value:  value,
			Column: obcluster.FieldObClusterID,
		})
	}
	if value, ok := ocuo.mutation.AddedObClusterID(); ok {
		_spec.Fields.Add = append(_spec.Fields.Add, &sqlgraph.FieldSpec{
			Type:   field.TypeInt64,
			Value:  value,
			Column: obcluster.FieldObClusterID,
		})
	}
	if value, ok := ocuo.mutation.GetType(); ok {
		_spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
			Type:   field.TypeString,
			Value:  value,
			Column: obcluster.FieldType,
		})
	}
	if value, ok := ocuo.mutation.RootserviceJSON(); ok {
		_spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
			Type:   field.TypeString,
			Value:  value,
			Column: obcluster.FieldRootserviceJSON,
		})
	}
	_node = &ObCluster{config: ocuo.config}
	_spec.Assign = _node.assignValues
	_spec.ScanValues = _node.scanValues
	if err = sqlgraph.UpdateNode(ctx, ocuo.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{obcluster.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{err.Error(), err}
		}
		return nil, err
	}
	return _node, nil
}
